<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title">当前城市</div>
        <div class="button-list">
          <div class="button-wrap">
            <div class="button">{{this.currentCity}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title">热门城市</div>
        <div class="button-list">
          <div class="button-wrap" v-for="city in hot" :key="city.id" @click="checkCity(city.name)">
            <div class="button">{{city.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(city, key) in cities" :key="key" :ref="key">
        <div class="title">{{key}}</div>
        <div class="item-list">
          <div
            class="item"
            v-for="eachcity in city"
            :key="eachcity.id"
            @click="checkCity(eachcity.name)"
          >{{eachcity.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import Bscroll from 'better-scroll'
export default {
  name: 'CityList',
  props: {
    hot: {
      type: Array,
      default: () => {
        return ''
      }
    },
    cities: {
      type: Object,
      default: () => {
        return ''
      }
    },
    letter: {
      type: String,
      default: () => {
        return ''
      }
    }
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  methods: {
    checkCity (city) {
      // alert(city)
      // this.$store.commit('changeCity', city)
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      // console.log(this.letter)
      // 监听点击字母的变化做出跳转
      if (this.letter) {
        const element = this.$refs[this.letter][0] // 数组转化成DOM元素
        // console.log(element)
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl'
.list
  overflow: hidden
  position: absolute
  top: 1.8rem
  left: 0
  bottom: 0
  right: 0
.title
  line-height: 0.54rem
  background: #eee
  padding-left: 0.2rem
  color: #666
  font-size: 0.26rem
  border-top: 1px solid #fff
  border-bottom: 1px solid #ccc
  border-top: 1px solid #ccc
.button-list
  padding: 0.1rem 0.6rem 0.1rem 0.1rem
  overflow: hidden
  .button-wrap
    float: left
    width: 33.33%
    .button
      padding: 0.1rem 0
      margin: 0.1rem
      text-align: center
      border: 0.02rem solid #ccc
      border-radius: 0.06rem
.item
  line-height: 0.76rem
  padding-left: 0.2rem
  border-bottom: 0.02rem solid #ccc
</style>
